/* Name: CreateWormholeBtn
 * Author: John Gideon
 * Date: 5-15-12
 * Description: Button used to create a wormhole in game when a planet is selected.
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;

public class CreateWormholeBtn extends Button {
	private int width;
	private int height;
	private Boolean visible = false;
	private Wormhole wid = null;
	
	// Creates a wormhole button at the given location
	public CreateWormholeBtn(int px, int py, int pwidth, int pheight) {
		super(px, py, pwidth, pheight);
		Game.create((Item)this, 1000000); // Needed for update, draw, and destroy
		colType = COL_NONE; // Needed for collision checking
		width = pwidth;
		height = pheight;
	}
	
	// Called when the button is pressed - sets the visual elements, and plays sound.
	protected void pressed() {
		Sound.effect(Sound.EFFECT_CLICK);
		Planet pid = GameScreen.getSelectedPlanet();
		if (pid != null) {
			wid = new Wormhole(pid);
			GameScreen.setButtonsVisible(false);
		} else {
			setSelected(false);
		}
	}
	
	// Called every time a new frame is drawn - draws the button when it is marked as visible
	public void draw() {
		if (visible) {
			Graphics2D gh = Game.getGraphicsHandle();
			if (btnDrawState == Button.BTN_STATE_OFF) {gh.setColor(Color.GRAY);}
			else if (btnDrawState == Button.BTN_STATE_HOVER) {gh.setColor(Color.LIGHT_GRAY);}
			else {gh.setColor(Color.DARK_GRAY);}
			gh.fillRoundRect(x, y, width, height, 15, 15);
			gh.setFont(new Font("Serif", Font.PLAIN, 20));
			gh.setColor(Color.BLACK);
			String text = "Create Wormhole";
			FontRenderContext frc = gh.getFontRenderContext();
			Rectangle2D rect = gh.getFont().getStringBounds(text, frc);
			gh.drawChars(text.toCharArray(), 0, text.length(), (int)(x + width/2 - rect.getWidth()/2), 
					(int)(y + height/2 + rect.getHeight()/2)-5);
		}
		Wormhole.drawCost();
	}
	
	public void update() {if (visible) {updateBtn();}}
	
	// Called when wormhole button is clicked on
	public void leftMousePress() {
		if (visible) {
			leftMousePressBtn();
		}
		if (wid != null) {
			if (wid.placeWormhole()) {
				GameScreen.setButtonsVisible(true);
				wid = null;
			}
		}
	}
	
	// Cancels the creation of a wormhole
	public void rightMousePress() {
		if (wid != null) {
			wid.cancelWormhole();
			setSelected(false);
			GameScreen.setButtonsVisible(true);
			wid = null;
		}
	}
	
	public void leftMouseRelease() {if (visible) {leftMouseReleaseBtn();}}
	
	// Sets the button to be visible
	public void setVisible(Boolean val) {
		visible = val;
		if (visible == false) {
			setSelected(false);
		}
	}
}